package cn.edu.gcu.ssl.system.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;


import com.mysql.fabric.xmlrpc.base.Data;

import net.sf.json.JSONObject;
public class HttpUtils {

	
	
	public static String post(String url,String data,String eid)throws Exception
	{
		HttpClient  client=new DefaultHttpClient();
		HttpPost post=new  HttpPost(url);
		String responsebody=null;	
		try {
			ResponseHandler<String> responseHandler=new BasicResponseHandler();
			if(!eid.equals(""))
			{	//NameValuePair 简单名称值对节点类型
				List<NameValuePair> nvps=new ArrayList<NameValuePair>();
				nvps.add(new BasicNameValuePair("nonce", String.valueOf(new Date().getTime())));
				nvps.add(new BasicNameValuePair("eid",eid));
				nvps.add(new BasicNameValuePair("data",data));
				// System.out.println(url+",eid:"+eid+",msg:"+message+","+nvps.toString());
				//设置编码
				post.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));
			} 
				responsebody=client.execute(post,responseHandler);
				JSONObject ret=JSONObject.fromObject(responsebody);
			}catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				System.out.println("错误"+e.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("连接错误：(HttpResponseException 错误)");
			}
			finally {
				client.getConnectionManager().shutdown();
			}
			return responsebody	;
		
	}
	
	
	
	
	
	
	
	
}
